export const urlToFile = async (url, fileName = "image.jpg") => {
  try {
    // 1. 发起网络请求获取图片
    const response = await fetch(url);

    // 2. 检查请求是否成功
    if (!response.ok) {
      throw new Error(
        `Failed to fetch image: ${response.status} ${response.statusText}`
      );
    }

    // 3. 将响应转换为 Blob
    const blob = await response.blob();

    // 4. 从 URL 或默认值推断文件扩展名
    const ext = fileName.split(".").pop() || "jpg";
    const mimeType = blob.type || `image/${ext}`;

    // 5. 创建 File 对象
    return new File([blob], fileName, {
      type: mimeType,
      lastModified: new Date().getTime(),
    });
  } catch (error) {
    console.error("Error converting URL to File:", error);
    throw error;
  }
};
